

let p1 = new Promise(res => {
    setTimeout(() => {
        res(1)
    }, 2000)
})

let p2 = new Promise(res => {
    setTimeout(() => {
        res(2)
    }, 1000)
})

let p3 = 3

let promises = [p1, p2, p3]

Promise.all(promises).then(res => {
    console.log(res) //  [1,2,3]
})

Promise.myAll = function (promises) {
    return new Promise((resolve, reject) => {
        let result = []
        let i = 0;
        const next = (value, index) => {
            result[index] = value
            if (++i === promises.length) {
                resolve(result)
            }
        }

        for (let i = 0; i < promises.length; i++) {
            let curPromise = promises[i]
            if (curPromise.then && typeof curPromise.then === 'function') {
                curPromise.then((value) => {
                    next(value, i)
                }, (e) => {
                    reject(e)
                })
            } else {
                next(curPromise, i)
            }

        }
    })
}

Promise.myAll(promises).then(res => {
    console.log(res) //  [1,2,3]
})